package ikar.view.components;

import ikar.nrd.fillingref.generator.XsltOutTemplates;
import ikar.nrd.model.FillingRef;
import ikar.nrd.model.XsltNodeType;

import javax.swing.tree.DefaultMutableTreeNode;

import org.apache.wicket.markup.html.basic.MultiLineLabel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.util.string.Strings;

public class XslOutTransformPanel extends Panel {



	public XslOutTransformPanel(String id, String xmlNodeName, DefaultMutableTreeNode rootNode) {
		super(id);
		if (Strings.isEmpty(xmlNodeName)) {
			add(new MultiLineLabel("multiLineLabel", "Xml Element Name can not be embty!!!!").setEscapeModelStrings(false));
			return;
		}

		DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) rootNode.getRoot();
		FillingRef fillRef = ((FillingRef) parentNode.getUserObject());
		String content = XsltOutTemplates.getContent(xmlNodeName, getNodesIerarchy(rootNode, "").toString());
		add(new MultiLineLabel("multiLineLabel", content.toString()).setEscapeModelStrings(false));
		// StringBuilder content = new StringBuilder(header);
		// content.append("&lt;xsl:template match=\"main\"&gt;\n");
		// content.append("&lt;" + fillRef.getXpath() + nameSpaces + "&gt;\n");
		// content.append(getNodesIerarchy(rootNode, ""));
		// content.append("&lt;/" + fillRef.getXpath() + "&gt;\n");
		// content.append("&lt;/xsl:template&gt;\n&lt;/xsl:stylesheet&gt;\n");
		// add(new MultiLineLabel("multiLineLabel", content.toString()).setEscapeModelStrings(false));

	}

	private StringBuilder getNodesIerarchy(DefaultMutableTreeNode rootNode, String tab) {
		StringBuilder strBuilder = new StringBuilder();

		for (int i = 0; i < rootNode.getChildCount(); i++) {
			DefaultMutableTreeNode node = (DefaultMutableTreeNode) rootNode.getChildAt(i);
			FillingRef fillingRef = (FillingRef) node.getUserObject();

			// if node is composit (is not simple text node)
			if (rootNode.getChildAt(i).getChildCount() > 0) {

				if (XsltNodeType.FOREACH.getName().equals(fillingRef.getXsltNodeType())) {
					strBuilder.append("&lt;xsl:for-each select=\"" + fillingRef.getPlateCode() + "\"&gt;\n");
				}
				strBuilder.append(tab + "&lt;" + fillingRef.getXpath() + "&gt;\n");
				tab += "    ";
				strBuilder.append(getNodesIerarchy(node, tab));
				tab = tab.replaceFirst("    ", "");
				strBuilder.append(tab + "&lt;/" + fillingRef.getXpath() + "&gt;\n");
				if (XsltNodeType.FOREACH.getName().equals(fillingRef.getXsltNodeType())) {
					strBuilder.append("&lt;/xsl:for-each&gt;\n");
				}
			} else if (fillingRef.isAttribute()) {
				strBuilder.append("&lt;xsl:attribute name=\"" + fillingRef.getXpath().replace("@", "") + "\"&gt;\n");
				strBuilder.append("    &lt;xsl:value-of select=\"" + fillingRef.getPlateCode() + "\" /&gt;\n");
				strBuilder.append("&lt;/xsl:attribute&gt;\n");

			} else {
				strBuilder.append(tab + "&lt;" + getOpenTageValue(fillingRef.getXpath()) + "&gt;\n");
				strBuilder.append(tab + "    &lt;xsl:value-of select=\"" + getValue(node) + "\" /&gt;\n");
				strBuilder.append(tab + "&lt;/" + getCloseTageValue(fillingRef.getXpath()) + "&gt;\n");
			}

		}
		return strBuilder;
	}

	private String getOpenTageValue(String tagName) {
		return tagName.replace("[", " ").replace("]", " ").replace("''", "'");
	}

	private String getCloseTageValue(String tagName) {
		if (tagName.contains("[")) {
			return tagName.substring(0, tagName.indexOf("["));
		}
		return tagName;
	}

	private String getValue(DefaultMutableTreeNode node) {
		FillingRef fillRef = (FillingRef) node.getUserObject();
		if (fillRef.isSelected()) {
			return fillRef.getPlateCode();
		} else {
			return "<font color=red size=3>" + fillRef.getPlateCode() + "</font>";
		}
	}
}
